const http = require('http');
const logger = require('../../utils/log');
const querystring = require('querystring');

const server = http.createServer((request, response) => {
    // const url = request.url;
    const urlObj = new URL(url, 'http://localhost:8080');

    response.writeHead(200, {
        'content-type': 'application/json;charset=utf-8',
        'Access-Control-Allow-Origin': '*'
    })
    // logger.debug(url)
    let data = '';
    request.on('data', (chunk) => {
        data += chunk;
        logger.debug(chunk)
    })
    request.on('end', () => {
        switch (urlObj.pathname) {
            case '/api/list':
                response.end(JSON.stringify(querystring.parse(data)));
                break;
        
            default:
                response.end('not found');
                break;
        }
    })
})

server.listen(8080, () => {
    console.log('localhost:8080');
})